yjx.controller('withdrawController',['$rootScope','$scope','$fetch','modalService','$timeout','$state','$interval','$init',function ($rootScope,$scope,$fetch,modalService,$timeout,$state,$interval,$init) {
    /*模拟数据以下*/
    $scope.brokerage = '';
    $scope.totalBrokerage = 0;
    $init.title("提现");
    /*模拟数据以上*/
     if($rootScope.userId){
         $fetch.fetch(
             '/WeiXinPay/sureDrawingMoney',
             {
                 params:{
                     userId:$rootScope.userId
                 },
                 type:'POST'

             }
         ).then(function(data){
                 $timeout(function(){
                     if(data){
                         $scope.totalBrokerage = data.sureDrawingMoney;
                     }
                 })
             }).catch(function(e){
                 modalService.mainMsgError(e.msg);
             })
     }

    $scope.withdraw = function () {
        if(!$scope.totalBrokerage) {
            /**/
            modalService.mainMsgError("您当前没有可提现金额");
            return
        }
        if(!$scope.brokerage){
            modalService.mainMsgError("请输入您要提现的金额");
            return
        }
        if(!($scope.brokerage%1 == 0)){
            modalService.mainMsgError("提现金额必须为整数");
            return
        }
        if($scope.brokerage && $scope.brokerage>$scope.totalBrokerage) {
            modalService.mainMsgError("金额超过当前可提现金额");
            return
        }
        if(!$scope.code) {
            modalService.mainMsgError("请输入验证码");
            return
        }
       $fetch.fetch(
           '/WeiXinPay/drawingMoney',
           {
               params:{
                   userId:$rootScope.userId,
                   totalFee:$scope.brokerage,
                   code:$scope.code
               },
               type:'post'
           }
       ).then(function(data){
               $timeout(function(){
                   $scope.totalBrokerage - $scope.brokerage
                   modalService.mainMsgInfo("提现成功");
               })
           }).catch(function(e){

               modalService.mainMsgError(e.msg);
           })
    }

/*预备方案*/
    $scope.hintMsg = "尊敬的健康管理顾问，感谢您使用基因团，由于腾讯的技术问题，自动提现功能暂时不能使用。"
    $scope.hintMsg2="现在，请点击“我要提现”按钮，我们会将健康管理费直接打入您的支付宝账户，敬请谅解。祝业绩长虹。"
    var APIsendCode = '/validCode/send';
    var sendText = "获取验证码"
    $scope.sendText ="获取验证码";
    $scope.withdrawBak = function(){
        if(!$scope.totalBrokerage || $scope.totalBrokerage <=0){
            modalService.mainMsgInfo("您当前没有可提现管理费");
            return;
        }
        if(!$rootScope.alipayAccount){
            modalService.mainMsgInfo("请完善您的支付宝账号设置");
            $state.go("profile")
        }else{
            /*展示二维码*/
            modalService.wxCodeModal();
        }
    }

    $scope.sendCode = function(){
        if(!$scope.totalBrokerage || $scope.totalBrokerage <=0){
            modalService.mainMsgInfo("您当前没有可提现管理费");
            return;
        }
        if(!$rootScope.alipayAccount){
            modalService.mainMsgInfo("请完善您的支付宝账号设置");
            $state.go("profile")
        }else{
            /*发送验证码*/
            sendMsgCode();
        }
    }
    $scope.tagStatus = false;
    function sendMsgCode(){
        $fetch.fetch(
            APIsendCode, //
            {
                params: {userId: $rootScope.userId},
                cache: false,

            }
        ).then(function (data) {
                //验证码已发送注意查收
                //定时器timer
                var timer = 120;
                var time = timer;
                $timeout(function () {
                    //重置定时器对象
                    typeof($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                    $scope.sendStatus = 'sending';//发送页面
                    $scope.tagStatus = true;//按钮状态 改变为disable
                    $scope.sendText = '获取(' + time + ')';
                });
                $timeout(function () {
                    //定义定时器对象
                    $scope.tagTimer = $interval(function () {
                        time--;
                        if (time > 0) {
                            $scope.sendText = '获取(' + time + ')';
                        } else {
                            //time=timer;
                            $scope.tagStatus = false;
                            $scope.sendText = sendText;
                            return;
                        }
                        //console.info(time);
                    }, 1000, time);
                });
            }).catch(function (e) {
                $timeout(function () {
                    typeof($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                    modalService.mainMsgError(e.msg);
                    $scope.tagStatus = false;
                    $scope.sendText = sendText;
                    return;
                });
            });
    }


}])